package com.android.vestel.photoroid;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FlickrFormActivity extends Activity {
	EditText unameEditText;
	String username = null;
	DataHelper dataHelper = null;
	String ProgramState = null;
	String TAG = "FlickrFormActivity";
	boolean isUserValid = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flickr);
		unameEditText = (EditText) findViewById(R.id.F_UsernameEditText);
		Button signInButton = (Button) findViewById(R.id.F_signIn);

		signInButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Thread th = new Thread(new Runnable() {

					@Override
					public void run() {
						Log.e(TAG, "Button is clicked.");
						username = unameEditText.getText().toString().trim();
						Log.e(TAG, "Entered username is " + username);
						if (username == null) {
							Log.e(TAG, "username null");

						} else {
							// TODO username validation
							final String username2 = new String(username);
							FlickrClient client = new FlickrClient(username2);
							if (client.is_valid()) {
								// incase the service is running
								stopService(new Intent(getBaseContext(),
										PhotoroidService.class));
								Log.e(TAG, "username is valid.");
								updateProgramState();
								if (ProgramState
										.equals(PhotoroidService.PROG_STAT_PICASA_DISABLED)
										|| ProgramState
												.equals(PhotoroidService.PROG_STAT_PICASA_ENABLED)) {
									openDBConnection();
									Log.e(TAG,
											"All photos deleted from photos table");
									dataHelper.deleteAllPhotos();
									File photoroidDir = new File(SyncPhotos.downloadDir);
									
									if(photoroidDir.exists()){
										File[] flist = photoroidDir.listFiles();
										for(File f:flist){
											if(f.isFile()){
												Log.e(TAG,"File deleted = "+f.getAbsolutePath());
												f.delete();
											}
										}
										photoroidDir.delete();
									}
									closeDBConnection();
								}
								openDBConnection();
								dataHelper.updateAccountTable(username, "null");
								Log.e(TAG,"username is inserted to database "+username);
								updateProgramState(
										PhotoroidService.PROG_STAT_FLICKR_ENABLED,
										"flickr");
								// start sevice (again)
								startService(new Intent(getBaseContext(),
										PhotoroidService.class));
								setResult(0);
								finish();
							} else {
								Log.e(TAG, "User is not valid");
								// TODO alertbox
							}
						}
					}
				});
				th.start();

			

			}
		});
		
		// if there was a flickr account
		updateProgramState();
		Log.e(TAG, "ProgramState is " + ProgramState);
		// if any user details exist before show them on
		openDBConnection();
		if (ProgramState
				.equals(PhotoroidService.PROG_STAT_FLICKR_ENABLED)
				|| ProgramState
						.equals(PhotoroidService.PROG_STAT_FLICKR_DISABLED)
				|| ((!dataHelper.selectAllOfAccounts()
						.get(DataHelper.USERNAME).equals("null")) && dataHelper
						.selectAllOfAccounts().get(DataHelper.PASSWORD)
						.equals("null"))) {
			Log.e(TAG, "There is flickr data in DB");
			username = dataHelper.selectAllOfAccounts().get(
					DataHelper.USERNAME);
			closeDBConnection();
			Log.e(TAG, "Username=" + username);

			if (username != "null") {
				Log.e(TAG, "Username or Password is not null");

				unameEditText.setText(username);
			} else
				Log.e(TAG, "Username is null so not shown");

		} else {
			Log.e("PicasaFormActivity", "There is no flikcr data in DB");
			username = "";
			unameEditText.setText(username);
		}
	}

	@Override
	public void onPause() {
			setResult(0);
			finish();
		super.onPause();
	}

	private void openDBConnection() {
		if (dataHelper == null) {
			dataHelper = new DataHelper(this);
		}
		Log.e("PicasaFormActivity", "Database Connection Opened");
	}

	private void closeDBConnection() {
		if (dataHelper != null) {
			dataHelper.closeDB();
			dataHelper = null;
		}
		Log.e("PicasaFormActivity", "Database Connection Closed");

	}

	private String updateProgramState() {
		openDBConnection();
		ProgramState = dataHelper.selectAllOfSetting().get(
				DataHelper.PROGRAM_STATE);
		closeDBConnection();
		return ProgramState;
	}

	private void updateProgramState(String newProgramState,
			String newServiceProvider) {
		ProgramState = newProgramState;
		openDBConnection();
		dataHelper.updateSettingTable(newProgramState, newServiceProvider);
		closeDBConnection();
	}
}
